﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL.Forms;
using UCL.Data;
using UCL;

namespace DBKit5
{
    public partial class frmModuleFieldEdit : Form
    {
        public Dictionary<string, object> FieldData { get; set; }
        public string DbProviderName { get; set; }

        private string[] DbTypesOfLengthly;
        private string oldName;
        public frmModuleFieldEdit()
        {
            InitializeComponent();
        }

        private void frmModuleFieldEdit_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DbProviderName))
            {
                MessageBox.Show("数据库类型无效");
                this.Close();
                return;
            }
            GeneralDataTypeHelper gdth = new GeneralDataTypeHelper();
            cbbType.DataSource = gdth.GetDbTypes(DbProviderName);
            DbTypesOfLengthly = gdth.GetDbTypesOfLengthly(DbProviderName);
            this.FormLoad();
        }
        private void FormLoad()
        {
            if (FieldData == null) return;
            oldName = FieldData["Name"].ToString();
            tbName.Text = oldName;
            tbDefault.Text = FieldData["Default"].ToString();
            tbExtra.Text = FieldData["Extra"].ToString();
            cbbType.SelectedItem = FieldData["Type"].ToString();
            tbLength.Text = FieldData["Length"].ToString();
            chkKey.Checked = FieldData["Key"].ToString().ToBoolean();
            chkNull.Checked = FieldData["Null"].ToString().ToBoolean();
            tbComment.Text = FieldData["Comment"].ToString();
        }

        public void SetTypeValue(string s)
        {
            cbbType.SelectedItem = s;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbLength.TextLength > 0 && tbLength.Text.Contains('%'))
            {
                MessageBox.Show("字段长度未定义");
                return;
            }
            frmModuleList frm = (frmModuleList)this.Owner;
            frm.UpdateFieldData(tbName.Text, cbbType.SelectedItem.ToString(), tbLength.Text, tbDefault.Text, chkKey.Checked, chkNull.Checked, tbExtra.Text, tbComment.Text, oldName);
            this.Close();
        }

        private void btnPickType_Click(object sender, EventArgs e)
        {
            this.OpenDialog<frmModuleDataTypePicker>(() => new frmModuleDataTypePicker() { DataTypeName = cbbType.SelectedItem.ToString(), Length = tbLength.Text, DbProviderName = DbProviderName });
        }

        private void cbbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbType.SelectedItem == null || DbTypesOfLengthly == null) return;
            if (DbTypesOfLengthly.Contains(cbbType.SelectedItem.ToString()))
            {
                tbLength.Text = "%";
            }
            else
            {
                tbLength.Clear();
            }
        }

        private void cbbDefault_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbDefault.SelectedIndex == -1) return;
            switch (cbbDefault.SelectedItem.ToString())
            {
                case "now()":
                    Func_Default_Now();
                    break;
                default:
                    tbDefault.Text = cbbDefault.SelectedItem.ToString();
                    tbExtra.Clear();
                    break;
            }
        }
        private void Func_Default_Now()
        {
            DatabaseProduct dbp= new GeneralDataTypeHelper().GetDatabaseProvider(this.DbProviderName);
            switch (dbp)
            {
                case DatabaseProduct.SQLServer:
                    tbDefault.Text = "getdate()";
                    break;
                case DatabaseProduct.MySQL:                    
                    tbDefault.Text = "CURRENT_TIMESTAMP";
                    tbExtra.Text = "on update CURRENT_TIMESTAMP";
                    break;
                case DatabaseProduct.SQLite:
                    tbDefault.Text = "CURRENT_TIMESTAMP";
                    break;
                case DatabaseProduct.Access:
                    tbDefault.Text = "Now()";
                    break;
            }
        }
    }
}
